package com.roger.tool.utils;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;

import com.roger.tool.SGLog;

/**
 * @author lch
 * @version Create time：2016-12-23 类说明:
 */
public class XorFileUtil {

	/**
	 * 读取 assets 下的 配置文件，文件中 以键值对形式一行行保存数据,用等号分割，如 appid=101; 忽略
	 * #号开头的一行,因此可以用#开头的一行作为注释。
	 * 
	 * @param pContext
	 * @param pFileName
	 * @param isEncrypt
	 *            是否是加密过的
	 * @return
	 */
	public static Map<String, String> getConfigFromAssets(Context pContext, String pFileName, boolean isXorEncrypt) {
		try {
			List<String> configContentList = readConfig(pContext.getAssets().open(pFileName), isXorEncrypt);
			if (configContentList != null && configContentList.size() > 0) {
				Map<String, String> configContentMap = new HashMap<String, String>();
				for (String configContent : configContentList) {
					String[] configContentSplit = configContent.split("=");
					String key = configContentSplit[0].trim();
					String value = "";
					if (configContentSplit.length > 1) {
						value = configContentSplit[1].trim();
					}
					configContentMap.put(key, value);
				}
				return configContentMap;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param pInputStream
	 * @param isXorEncrypt
	 *            是否是加密过的
	 * @return
	 */
	public static List<String> readConfig(InputStream pInputStream, boolean isXorEncrypt) {
		try {

			if (isXorEncrypt) {
				// InputStream 转 String
				String encryptStr = inputStreamToString(pInputStream);
				// 异或解密
				String decodeStr = XorEncryptUtli.xorConvert(encryptStr, XorEncryptUtli.SECRET_KEY);
				SGLog.d("decodeStr=" + decodeStr);
				// 支付串转 InputStream
				pInputStream = new ByteArrayInputStream(decodeStr.getBytes());
			}

			InputStreamReader read = new InputStreamReader(pInputStream, "utf-8");
			BufferedReader reader = new BufferedReader(read);
			List<String> lineList = new ArrayList<String>();
			String line;
			while ((line = reader.readLine()) != null) {
				if (line.length() > 0) {
					if (!line.startsWith("#")) {
						lineList.add(line);
					}
				}
			}
			reader.close();
			read.close();
			return lineList;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static String readFileToString(String pFilePath) {

		String s = "";
		try {
			File file = new File(pFilePath);
			if (file.isFile() && file.exists()) { // 判断文件是否存在

				FileInputStream inStream = new FileInputStream(pFilePath);
				ByteArrayOutputStream bos = new ByteArrayOutputStream();
				byte[] buffer = new byte[1024];
				int length = -1;
				while ((length = inStream.read(buffer)) != -1) {
					bos.write(buffer, 0, length);
				}
				bos.close();
				inStream.close();
				s = new String(bos.toByteArray(), "UTF-8");

			} else {
				System.out.println("找不到指定的文件 ");
			}
		} catch (Exception e) {
			e.printStackTrace();
			System.out.println("读取文件内容出错");
		}
		return s;
	}

	public static String inputStreamToStr(InputStream inputStream) {

		String s = "";
		try {
			ByteArrayOutputStream outStream = new ByteArrayOutputStream();
			byte[] data = new byte[2048];
			int count = -1;
			while ((count = inputStream.read(data, 0, 2048)) != -1) {
				outStream.write(data, 0, count);
			}
			data = null;
			s = new String(outStream.toByteArray(), "UTF-8");

		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}

	public static String inputStreamToString(InputStream inputStream) {

		String s = "";
		try {
			byte[] buffer = new byte[2048];
			int readBytes = 0;
			StringBuilder stringBuilder = new StringBuilder();
			while ((readBytes = inputStream.read(buffer)) > 0) {
				stringBuilder.append(new String(buffer, 0, readBytes));
			}
			s = stringBuilder.toString();

		} catch (Exception e) {
			e.printStackTrace();
		}
		return s;
	}

}
